package com.github.itdachen.quartz.config;

import jakarta.annotation.Resource;
import org.quartz.spi.TriggerFiredBundle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import org.springframework.stereotype.Component;

/**
 * QuartzJobFactory
 *
 * @author 剑鸣秋朔
 * @date 2025/11/21 21:07
 */
@Component
public class QuartzJobFactory  extends SpringBeanJobFactory {
    private static final Logger logger = LoggerFactory.getLogger(QuartzJobFactory.class);

    @Resource
    private AutowireCapableBeanFactory beanFactory;

    /**
     * 这里覆盖了 super 的 createJobInstance 方法，对其创建出来的类再进行 autowire。
     * 没调度一次，该方法执行一次
     */
    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        // logger.info("Creating Quartz Job Instance ...");
        Object jobInstance = super.createJobInstance(bundle);
        beanFactory.autowireBean(jobInstance);
        return jobInstance;
    }

}
